// 添加Boostrap 4
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-vue/dist/bootstrap-vue.css";
import BootstrapVue from "bootstrap-vue";
import "ionicons-npm/css/ionicons.min.css";
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import { sync } from "vuex-router-sync";
import VueAxios from "vue-axios";
import axios from "axios";
import VueAuth from "@websanova/vue-auth";
import { save } from "src/service/api";
Vue.config.productionTip = false;
// 添加调试功能
Vue.config.devtools = true;
Vue.router = router;
Vue.use(VueAxios, axios);
Vue.use(VueAuth, {
  auth: {
    request: function(req, token) {
      this.options.http._setHeaders.call(this, req, {
        Authorization: `Bearer ${window.localStorage.getItem("auth-token")}`
      });
    },
    response: function(res) {
      // 表示Token是成功的
      // 如果
      if (!store.getters.user.isLoggedIn) {
        save(`http://${process.env.VUE_APP_BACKEND_URL}:8080/auth/user`, {
          token: window.localStorage.getItem("auth-token")
        })
          .then(response => {
            if (response.status == "200") {
              store.dispatch("loggedIn");
              store.dispatch("setUser", response.data);
            }
          })
          .catch(error => {
            store.dispatch("loggingIn");
            console.log(error);
          });
      }
      return res;
    }
  },
  http: require("@websanova/vue-auth/drivers/http/axios.1.x.js"),
  router: require("@websanova/vue-auth/drivers/router/vue-router.2.x.js"),
  loginData: {
    url: `http://${process.env.VUE_APP_BACKEND_URL}:8080/auth/login`,
    fetchUser: false
  },
  refreshData: {
    enabled: false
  }
});
// Effortlessly keep vue-router and vuex store in sync.
// 毫不费力地保持vue-router 和 vuex的store的同步
sync(store, router);
Vue.use(BootstrapVue);
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
